#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time       :   21:56
# Author     : Y4
# Site       : 
# File       : 004 正则函数.py
# Software   : PyCharm


import re
# re.match()                从开头查找
# re.search()               查找出现的第一个
# re.findall()              查找所有
# re.sub()                  替换
# re.split()                切割


phone = '010-123456789'
result = re.match(r'(\d{3}|\d{4})-(\d{9})$',phone)
print(result.group())                           # 010-123456789
print(result.group(1))                          # 010
print(result.group(2))                          # 123456789


msg = 'fdgu5ji1uk3212d2g34y475f1g654y325122d43t46y5'
result = re.search('[a-z][0-9][a-z]',msg)
print(result)                                           # <_sre.SRE_Match object; span=(3, 6), match='u5j'>


result = re.findall('[a-z][0-9][a-z]',msg)
print(result)                                           # ['u5j', 'i1u', 'd2g', 'f1g']


# 替换 (可函数)
def func(temp):
    num = temp.group()
    num1 = int(num) + 1
    return str(num1)
result = re.sub(r'\d+','你好啊','java100,python100')
print(result)                                   # java你好啊,python你好啊
result = re.sub(r'\d+',func,'java90,python100')
print(result)                                   # java91,python101


# 切割
result = re.split(r'[:,]','java100,python100')
print(result)                                   # ['java100', 'python100']
